﻿Public Class clsRandCheckHistory
    Private m_cData As New clsDbCondData
    Public Function selectRandCheckHistory(lotNo As String, ProcCD As String, EquipNo As String, RackNo As String, Status As String) As DataTable
        Dim szSql As String = ""

        szSql &= "SELECT LOTNO,PROCCD,RACKNO,EQUIPNO,USERNO,INVNUM,CHECKNUM,FRAMENUM,NGNUMDEF,NGNUM,STATUS,CHECKTYPE,CHECKOPE,CHECKDATE "
        szSql &= "FROM PER.L_RANDCHECK_HISTORY WHERE "

        If lotNo <> "" Then
            szSql &= "LOTNO = '" & lotNo & "' AND "
        End If
        If ProcCD <> "" Then
            szSql &= "PROCCD = '" & ProcCD & "' AND "
        End If
        If EquipNo <> "" Then
            szSql &= "EQUIPNO = '" & EquipNo & "' AND "
        End If
        If RackNo <> "" Then
            szSql &= "RACKNO = '" & RackNo & "' AND "
        End If

        If Status = "" Then
            szSql &= "STATUS IS NULL AND "
        ElseIf Status <> "ALL" Then
            szSql &= "STATUS = '" & Status & "' AND "
        End If

        szSql &= "1=1"
        szSql &= " ORDER BY LOTNO "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        If dt Is Nothing OrElse dt.Rows.Count = 0 Then
            dt = Nothing
        End If
        Return dt
    End Function

    Public Sub updateStatus(lotNo As String, procCD As String, rackNo As String, NGNum As String, status As String, OPE As String)
        Dim szSql As String = ""

        szSql &= "UPDATE PER.L_RANDCHECK_HISTORY SET "
        szSql &= " STATUS = '" & status & "' ,"
        szSql &= " NGNUM = " & NGNum & ", "
        szSql &= " CHECKOPE = '" & OPE & "',  "
        szSql &= " CHECKDATE = '" & m_cData.GetServerTime() & "',"
        szSql &= " UPDATE_TIME = '" & m_cData.GetServerTime() & "'"
        szSql &= " WHERE  LotNo = '" & lotNo & "' AND "
        szSql &= " PROCCD = '" & procCD & "' "
        If rackNo <> "" Then
            szSql &= " AND RACKNO = '" & rackNo & "' "
        End If
        DBacs.ExecuteUpdateSql(szSql, -1)
    End Sub
    Public Sub updateRackNo(lotNo As String, procCD As String, oldrackNo As String, newRackNO As String)

        Dim szSql As String = ""
        szSql = ""
        szSql &= "SELECT * FROM  PER.L_RANDCHECK_HISTORY  "
        szSql &= " WHERE  LotNo = '" & lotNo & "' AND "
        szSql &= " PROCCD = '" & procCD & "' AND "
        szSql &= " RackNo = '" & newRackNO & "' "
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        If dt Is Nothing OrElse dt.Rows.Count = 0 Then

            szSql = ""
            szSql &= "UPDATE PER.L_RANDCHECK_HISTORY SET "
            szSql &= " RackNo = '" & newRackNO & "' "
            szSql &= " ,UPDATE_TIME = '" & m_cData.GetServerTime() & "'"
            szSql &= " WHERE  LotNo = '" & lotNo & "' AND "
            szSql &= " PROCCD = '" & procCD & "' "
            If oldrackNo <> "" Then
                szSql &= " AND RACKNO = '" & oldrackNo & "' "
            End If
            DBacs.ExecuteUpdateSql(szSql, -1)

        End If
    End Sub
    Public Sub insertData(LotNo As String,
                        ProcCD As String,
                        RackNo As String,
                        EquipNo As String,
                        UserNo As String,
                        INVNUM As Integer,
                        CheckNum As Integer,
                        FrameNum As Integer,
                        NGNumDef As Integer,
                        NGNUM As Integer,
                        Status As String,
                        CheckType As String
                          )
        delData(LotNo, ProcCD, RackNo)
        'Status = OK
        Dim szSql As String = ""

        szSql = ""
        szSql &= "INSERT INTO PER.L_RANDCHECK_HISTORY ("
        szSql &= "LOTNO,PROCCD,RACKNO,EQUIPNO,USERNO,INVNUM,CHECKNUM,"
        szSql &= "FRAMENUM,NGNUMDEF,NGNUM,STATUS,CHECKTYPE,UPDATE_TIME) "
        szSql &= "VALUES ( "
        szSql &= String.Format("'{0}', '{1}', '{2}','{3}', '{4}', {5},{6}, {7}, {8},{9},'{10}','{11}','{12}'",
                               LotNo, ProcCD, RackNo, EquipNo, UserNo, INVNUM, CheckNum, FrameNum, NGNumDef, NGNUM, Status, CheckType, m_cData.GetServerTime())
        szSql &= ")"

        DBacs.ExecuteUpdateSql(szSql, -1)

    End Sub

    Public Sub delData(LotNo As String, ProcCD As String, RackNo As String）
        Dim szSql As String = ""

        szSql = ""
        szSql &= "delete From PER.L_RANDCHECK_HISTORY Where "
        szSql &= "LotNo = '" & LotNo & "' and PROCCD = '" & ProcCD & "' and RackNo = '" & RackNo & "'"
        DBacs.ExecuteUpdateSql(szSql, -1)
    End Sub
End Class
